//
//  SSSHomeViewController.m
//  SSSMerchant
//
//  Created by Darker on 2017/3/23.
//  Copyright © 2017年 xnxin. All rights reserved.
//

#import "SSSHomeViewController.h"
#import "SSSPersonnalCell.h"
#import "SSSRecordOrderViewController.h"
#import "ZDScanningQRCodeVC.h"
#import "SSSSystemMessageViewController.h"
#import "SSSPaymentCodeViewController.h"
#import "SSSMyMemberViewController.h"
#import "SSSMerchantManagementViewController.h"
#import "SSSDiscountViewController.h"
#import "SSSSelectedBusinessView.h"

@interface SSSHomeViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
    //数据源数组
    NSArray *_nameArray;
    NSArray *_imageNameArry;
    UIButton *_selectShopBtn;

}


@end

static NSString * const HomeID = @"homeCell";

@implementation SSSHomeViewController

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    self.view.backgroundColor = [UIColor colorWithHex:@"F9F9FA"];
//    self.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
//    self.tabBarController.hidesBottomBarWhenPushed = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initDataSourceArray];

    //初始化界面
    [self initAppearance];
    
}
#pragma mark - 初始化数据源数组
- (void)initDataSourceArray{
    
    _nameArray = @[@"订单",@"我的会员",@"店铺管理",@"收款码", @"系统消息"];
    _imageNameArry = @[@"icon_home_01",@"icon_home_02",@"icon_home_03",@"icon_home_04",@"icon_home_05"];
    
}
#pragma mark - 初始化界面
- (void)initAppearance{
    
    UIImageView *headerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, KSCREEN_Width, HeigthIos6CompeteWithCurrent(330))];
    headerImageView.image = [UIImage imageNamed:@"img_home_bg"];
    headerImageView.userInteractionEnabled = YES;
    [self.view addSubview:headerImageView];
    
    //扫面按钮
    UIButton *scanBtn = [[UIButton alloc]init];
    [headerImageView addSubview:scanBtn];
    [scanBtn setBackgroundImage:[UIImage imageNamed:@"icon_home_00"] forState:UIControlStateNormal];
    [scanBtn addTarget:self action:@selector(scanBtnClick) forControlEvents:UIControlEventTouchDown];
    scanBtn.sd_layout
    .topSpaceToView(headerImageView,HeigthIos6CompeteWithCurrent(34))
    .rightSpaceToView(headerImageView,WidthIos6CompeteWithCurrent(15))
    .widthIs(WidthIos6CompeteWithCurrent(22))
    .heightIs(HeigthIos6CompeteWithCurrent(21));
    
    UIImageView *userHeadImageView = [UIImageView new];
    //userHeadImageView.backgroundColor = [UIColor redColor];
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",@"http://quantuanle.ssstc.cn",[SSSUserManager getUserModel].avatar]];
    
    [userHeadImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"icon_headportrait"]];
    [headerImageView addSubview:userHeadImageView];
    userHeadImageView.sd_layout
    .centerXEqualToView(headerImageView)
    .topSpaceToView(headerImageView,HeigthIos6CompeteWithCurrent(114))
    .widthIs(WidthIos6CompeteWithCurrent(130))
    .heightIs(WidthIos6CompeteWithCurrent(131));
    
    //切圆角
    userHeadImageView.layer.masksToBounds = YES;
    userHeadImageView.layer.cornerRadius = userHeadImageView.frame.size.height/2;
    
    //箭头
    UIButton *rowBtn = [UIButton new];
    [rowBtn setBackgroundImage:[UIImage imageNamed:@"icon_arrow"] forState:UIControlStateNormal];
    [headerImageView addSubview:rowBtn];
    rowBtn.sd_layout
    .rightSpaceToView(headerImageView,WidthIos6CompeteWithCurrent(20))
    .bottomSpaceToView(headerImageView,HeigthIos6CompeteWithCurrent(20))
    .widthIs(WidthIos6CompeteWithCurrent(14))
    .heightIs(HeigthIos6CompeteWithCurrent(10));
    [rowBtn addTarget:self action:@selector(selectShopClick) forControlEvents:UIControlEventTouchDown];
    
    //选择店铺按钮
    _selectShopBtn = [UIButton new];
    [_selectShopBtn setTitle:@"请选择店铺" forState:UIControlStateNormal];
    [_selectShopBtn setTitleColor:[UIColor colorWithHex:@"FFFFFF"] forState:UIControlStateNormal];
    _selectShopBtn.titleLabel.font = [SSSFont systemFontOfSize:14];
    [headerImageView addSubview:_selectShopBtn];
    _selectShopBtn.sd_layout
    .centerYEqualToView(rowBtn)
    .rightSpaceToView(rowBtn,WidthIos6CompeteWithCurrent(5))
    .heightIs(HeigthIos6CompeteWithCurrent(30))
    .leftSpaceToView(headerImageView,0);
    _selectShopBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [_selectShopBtn addTarget:self action:@selector(selectShopClick) forControlEvents:UIControlEventTouchDown];
   
    
    //添加下面的collectionView
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //0, 0, 126, 112
    layout.itemSize = CGSizeMake(FSCREEN_Width / 3,(FSCREEN_Height - HeigthIos6CompeteWithCurrent(330) - SSSTabBarH)/3);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, HeigthIos6CompeteWithCurrent(330), FSCREEN_Width, (FSCREEN_Height - HeigthIos6CompeteWithCurrent(330) - SSSTabBarH)/3*2) collectionViewLayout:layout];
    collectionView.backgroundColor = [UIColor clearColor];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.scrollEnabled = NO;
    collectionView.indicatorStyle = UIScrollViewIndicatorStyleDefault;
    [collectionView registerNib:[UINib nibWithNibName:@"SSSPersonnalCell" bundle:nil] forCellWithReuseIdentifier:HomeID];
    [self.view addSubview:collectionView];

    
    
    
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return _nameArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    SSSPersonnalCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:HomeID forIndexPath:indexPath];
    cell.nameLabel.text = _nameArray[indexPath.row];
    cell.iconImageView.image = [UIImage imageNamed:_imageNameArry[indexPath.row]];
    if (indexPath.section == 0) {
        
        if (indexPath.row >= 3) {
            
            cell.topLine.hidden = YES;
            
        }
        
    }
    return cell;
}
#pragma mark -- 选中每一个单元格的处理
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    
    
    if ([_selectShopBtn.currentTitle isEqualToString:@"请选择店铺"]) {
        
        showTipHudStr(@"请选择店铺");
        return;
    }
    
    if (indexPath.row == 0) {
        
        //[self PushRecordOrderViewController];
        [self PushDiscountViewController];
        
    }else if (indexPath.row == 1){
        
        [self PushMyMemberViewController];
        
    }else if (indexPath.row == 2){
        
        [self PushStoreManagementViewController];
        
    }else if (indexPath.row == 3){
        
        [self PushGatheringCodeController];
        
    }else if (indexPath.row == 4){
        
        [self PushSystemMessageViewController];
        
    }
    
}
#pragma mark -- 点击选中跳转
//录单
- (void)PushRecordOrderViewController{
    
    SSSRecordOrderViewController *vc=[SSSRecordOrderViewController new];
    [self.navigationController pushViewController:vc animated:YES
     ];
}
//我的会员
- (void)PushMyMemberViewController{
    
    SSSMyMemberViewController *myMemberVc = [[SSSMyMemberViewController alloc]init];
    [self.navigationController pushViewController:myMemberVc animated:YES];
}

//店铺管理
- (void)PushStoreManagementViewController{
    
    UIViewController *vc = [SSSMerchantManagementViewController storyboardInstance];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

//收款码
- (void)PushGatheringCodeController{
    
    SSSPaymentCodeViewController *paymentCodeVc = [[SSSPaymentCodeViewController alloc]init];
    [self.navigationController pushViewController:paymentCodeVc animated:YES];
}
//折扣
- (void)PushDiscountViewController{
    
    SSSDiscountViewController *discountVc = [[SSSDiscountViewController alloc]init];
    [self.navigationController pushViewController:discountVc animated:YES];
    
}

//系统消息
- (void)PushSystemMessageViewController{

    UIViewController *vc = [SSSSystemMessageViewController new];
    [self.navigationController pushViewController:vc animated:true];
}
#pragma mark - 选择店铺按钮
- (void)selectShopClick{
    
    SSSSelectedBusinessView *dimView = [[SSSSelectedBusinessView alloc]initWithFrame:CGRectMake(0,0, KSCREEN_Width,FSCREEN_Height)];
    [dimView returnText:^(NSString *showText) {
       
        [_selectShopBtn setTitle:showText forState:UIControlStateNormal];
        
    }];
    dimView.ClickString = _selectShopBtn.currentTitle;
    [dimView showView];
}
#pragma mark - 扫面二维码按钮
- (void)scanBtnClick{
    
    // 1、 获取摄像设备
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if (device) {
        AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
        if (status == AVAuthorizationStatusNotDetermined) {
            [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
                if (granted) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        ZDScanningQRCodeVC *scanningQRCodeVC = [[ZDScanningQRCodeVC alloc] init];
                        [self.navigationController pushViewController:scanningQRCodeVC animated:YES];
                        NSLog(@"主线程 - - %@", [NSThread currentThread]);
                    });
                    NSLog(@"当前线程 - - %@", [NSThread currentThread]);
                    
                    // 用户第一次同意了访问相机权限
                    NSLog(@"用户第一次同意了访问相机权限");
                    
                } else {
                    
                    // 用户第一次拒绝了访问相机权限
                    NSLog(@"用户第一次拒绝了访问相机权限");
                }
            }];
        } else if (status == AVAuthorizationStatusAuthorized) { // 用户允许当前应用访问相机
            ZDScanningQRCodeVC *scanningQRCodeVC = [[ZDScanningQRCodeVC alloc] init];
            [self.navigationController pushViewController:scanningQRCodeVC animated:YES];
        } else if (status == AVAuthorizationStatusDenied) { // 用户拒绝当前应用访问相机
            QMUIAlertAction *action1 = [QMUIAlertAction actionWithTitle:@"取消" style:QMUIAlertActionStyleCancel handler:^(QMUIAlertAction *action) {
                
            }];
            QMUIAlertAction *action2 = [QMUIAlertAction actionWithTitle:@"确定" style:QMUIAlertActionStyleDestructive handler:^(QMUIAlertAction *action) {
                
            }];
            QMUIAlertController *alertController = [QMUIAlertController alertControllerWithTitle:@"警告" message: @"请去-> [设置 - 隐私 - 照片] 打开访问开关" preferredStyle:QMUIAlertControllerStyleAlert];
            [alertController addAction:action1];
            [alertController addAction:action2];
            [alertController showWithAnimated:YES];
            
        } else if (status == AVAuthorizationStatusRestricted) {
            NSLog(@"因为系统原因, 无法访问相册");
        }
    } else {
        
        
    }

}
#pragma mark - 内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
